package cn.hfatec.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: panwb
 *
 * Date: 2015/12/10
 * Time: 10:39
 */
public class DateUtil {

    /**
     *
     * 根据当前日期，获取该月份的第一一天
     *
     * @param currentDate
     * @return
     */
    public static Date getFirstTime(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     *
     * 根据当前日期获取该月份的最后一天
     *
     * @param currentDate
     * @return
     */
    public static Date getLastTime(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }


    /**
     *
     * 根据当前日期，获取该月份的第一一天
     *
     * @param currentDate
     * @return
     */
    public static Date getFirstDay(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     *
     * 根据当前日期获取该月份的最后一天
     *
     * @param currentDate
     * @return
     */
    public static Date getLastDate(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     *
     * 根据当前的时间，获取下周一的日期
     *
     * @param currentDate
     * @return
     */
    public static Date getFirstDateWeek(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        Integer currentWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_MONTH, 9 - currentWeekDay);

        return calendar.getTime();
    }

    public static Date getLastDateWeek(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        Integer currentWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_MONTH, 9 - currentWeekDay + 6);

        return calendar.getTime();
    }



    public static void main(String[] args) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dateFormat.format(getFirstDateWeek(dateFormat.parse("2015-12-29"))));
        System.out.println(dateFormat.format(getLastDateWeek(dateFormat.parse("2015-12-29"))));

    }

}
